/*********************************************************
 *********************************************************
 ********************                  *******************
 *************                                ************
 *******                  _oo0oo_                  *******
 ***                     o8888888o                     ***
 *                       88" . "88                       *
 *                       (| -_- |)                       *
 *                       0\  =  /0                       *
 *                     ___/`---'\___                     *
 *                   .' \\|     |// '.                   *
 *                  / \\|||  :  |||// \                  *
 *                 / _||||| -:- |||||- \                 *
 *                |   | \\\  -  /// |   |                *
 *                | \_|  ''\---/''  |_/ |                *
 *                \  .-\__  '-'  ___/-. /                *
 *              ___'. .'  /--.--\  `. .'___              *
 *           ."" '<  `.___\_<|>_/___.' >' "".            *
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |          *
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /          *
 *      =====`-.____`.___ \_____/___.-`___.-'=====       *
 *                        `=---='                        *
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      *
 *********__佛祖保佑__永无BUG__验收通过__钞票多多__*********
 *********************************************************/
package org.jbase.cms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.mapper.BaseMapper;
import org.jbase.cms.dao.CmsColumnsDao;
import org.jbase.cms.entity.CmsColumns;
import org.jbase.cms.ext.beetl.CmsFunctionPackage;
import org.jbase.common.service.EntityMapperService;
import org.jbase.common.utils.Pager;
import org.jbase.common.utils.message.Message;
import org.jbase.common.utils.message.MessageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Project: jbase-springboot <br/>
 * File: CmsColumnsService.java <br/>
 * Class: org.jbase.admin.service.CmsColumnsService <br/>
 * Description: 栏目表. <br/>
 * Copyright: Copyright (c) 2016 <br/>
 * Company: http://git.oschina.net/liuzp315/Jbase <br/>
 * Makedate: 2017-01-07 12:04:40 <br/>
 * gen by Jbase Coder <br/>
 * 
 * @author liuzhanhong
 * @version 1.0
 * @since 1.0
 */
@Service
public class CmsColumnsService extends EntityMapperService<CmsColumns> {

	@Autowired
	private CmsColumnsDao cmsColumnsDao;

	@Autowired
	private CmsFunctionPackage cmsFunctionPackage;

	@Override
	public BaseMapper<CmsColumns> dao() {
		return cmsColumnsDao;
	}

	public Pager query(Map<String, String> map, Pager pager) {

		PageQuery<Object> pageQuery = new PageQuery<Object>(pager.getPageNumber(), map, -1, pager.getPageSize());
		cmsColumnsDao.selectPager(pageQuery);

		pager.setList(pageQuery.getList());
		pager.setRecordCount((int) pageQuery.getTotalRow());
		return pager;
	}

	public CmsColumns fetchBySid(String sid) {
		return cmsColumnsDao.fetchBySid(sid);
	}

	public void deleteBySid(String sid) {
		cmsColumnsDao.deleteBySid(sid);
	}

	public Message<List<CmsColumns>> listBySids(String sids) {
		Message<List<CmsColumns>> mess = new Message<>();
		mess.SetMessageCode(MessageCode.SUCCESS);
		mess.setSuccess(true);

		if (StringUtils.isBlank(sids)) {
			return mess;
		}

		if (sids.startsWith(",")) {
			sids = sids.substring(1);
		}

		List<String> sqltemp = new ArrayList<>();
		String[] split = sids.split(",");
		for (String str : split) {
			if (StringUtils.isNotBlank(str))
				sqltemp.add(str);
		}

		Map<String, Object> param = new HashMap<>();
		param.put("sids", sqltemp);
		List<CmsColumns> listBySids = cmsColumnsDao.listBySids(param);

		mess.setData(listBySids);

		return mess;
	}

	//////// - 分割线，以上代码为自动生成 - ////////

	public Message<?> updateSort(String[] ids, String[] sorts) {

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		int length = ids.length;
		for (int i = 0; i < length; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", ids[i]);
			map.put("sort", sorts[i]);
			list.add(map);
		}

		int[] updateByIdBatch = cmsColumnsDao.updateSort(list);
		boolean result = updateByIdBatch.length == length ? true : false;
		if (result)
			cmsFunctionPackage.remave(CmsFunctionPackage.COLUMN_WITH_CHILDS);
		return Message.fireSuccess();
	}

	public List<Map<String, Object>> listSubMenus(int parentId) {
		List<CmsColumns> list = listByParentId(parentId);
		List<Map<String, Object>> lm = new ArrayList<Map<String, Object>>();
		if (list != null && list.size() > 0) {
			for (CmsColumns menu : list) {
				Map<String, Object> map = col2map(menu);
				List<Map<String, Object>> childs = listSubMenus(menu.getId());
				if (childs != null && childs.size() > 0) {
					map.put("children", childs);
					map.put("isParent", true);
				} else {
					map.put("isParent", false);
				}
				lm.add(map);
			}
			return lm;
		} else
			return null;
	}

	public List<CmsColumns> listByParentId(int parentId) {
		return cmsColumnsDao.selectByParentId(parentId);
	}

	/**
	 * 描述 : <描述函数实现的功能>. <br/>
	 * <p>
	 * 
	 * @param col
	 * @return
	 */
	private Map<String, Object> col2map(CmsColumns col) {
		Map<String, Object> map = new HashMap<>();
		map.put("id", col.getId());
		map.put("sid", col.getSid());
		map.put("name", col.getName());
		map.put("sort", col.getSort());
		map.put("_href", col.getHref());
		map.put("href", col.getHref());
		map.put("level", col.getLevel());
		map.put("showModes", col.getShowModes());
		map.put("parentId", col.getParentId());
		return map;
	}

	/**
	 * 
	 * 描述 : 列出所有顶级栏目以及其子栏目. <br/>
	 * <p>
	 * 
	 * @return
	 */
	public List<CmsColumns> listNavWithChilds() {
		List<CmsColumns> oneList = listNavTop();
		for (CmsColumns column : oneList) {
			column.set("childList", findChildListByParentId(column.getId()));
		}
		return oneList;
	}

	public List<CmsColumns> listNavTop() {
		return cmsColumnsDao.listNavTop();
	}

	public List<CmsColumns> listNavByParentId(int parentId) {
		return cmsColumnsDao.listNavByParentId(parentId);
	}

	public List<CmsColumns> findChildListByParentId(int parentId) {
		return cmsColumnsDao.findChildListByParentId(parentId);
	}

	/**
	 * 描述 : <描述函数实现的功能>. <br/>
	 * <p>
	 * 
	 * @param childId
	 * @return
	 */
	public List<CmsColumns> fetchByChildId(List<CmsColumns> list, int childId) {
		if (list == null)
			list = new ArrayList<CmsColumns>();
		CmsColumns childCol = cmsColumnsDao.unique(childId);
		list.add(childCol);
		int parentId = childCol.getParentId();
		if (parentId == 0) {
			return list;
		} else {
			return fetchByChildId(list, parentId);
		}
	}

	/**
	 * 描述 : <描述函数实现的功能>. <br/>
	 * <p>
	 * 
	 * @param articleId
	 */
	public CmsColumns fetchByArticleId(int articleId) {
		return cmsColumnsDao.fetchByArticleId(articleId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jbase.common.service.EntityMapperService#saveOrUpdate(java.util.Map)
	 */
	@Override
	public Message<CmsColumns> saveOrUpdate(Map<String, Object> map) {
		Message<CmsColumns> saveOrUpdate = super.saveOrUpdate(map);
		cmsFunctionPackage.remave(CmsFunctionPackage.COLUMN_WITH_CHILDS);
		return saveOrUpdate;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jbase.common.service.EntityMapperService#deleteById(java.lang.Object)
	 */
	@Override
	public Message<Integer> deleteById(Object key) {
		Message<Integer> deleteById = super.deleteById(key);
		cmsFunctionPackage.remave(CmsFunctionPackage.COLUMN_WITH_CHILDS);
		return deleteById;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jbase.common.service.EntityMapperService#deleteByIds(java.lang.String)
	 */
	@Override
	public Message<Integer> deleteByIds(String ids) {
		Message<Integer> deleteByIds = super.deleteByIds(ids);
		cmsFunctionPackage.remave(CmsFunctionPackage.COLUMN_WITH_CHILDS);
		return deleteByIds;
	}

	public CmsColumns fetchOne(String pid) {
		if (StringUtils.isBlank(pid) || "0".equals(pid))
			return cmsColumnsDao.fetchFirst();
		else
			return cmsColumnsDao.unique(pid);
	}
}
